home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 127_01 / rap4.c < prev    next >
Text File  |  1985-03-09  |  19KB  |  694 lines

  1. /*********************************************************************\
  2. ** .---------------------------------------------------------------. **
  3. ** |                                                               | **
  4. ** |                                                               | **
  5. ** |         Copyright (c) 1981, 1982, 1983 by Eric Martz.         | **
  6. ** |                                                               | **
  7. ** |                                                               | **
  8. ** |       Permission is hereby granted to use this source         | **
  9. ** |       code only for non-profit purposes. Publication of       | **
  10. ** |       all or any part of this source code, as well as         | **
  11. ** |       use for business purposes is forbidden without          | **
  12. ** |       written permission of the author and copyright          | **
  13. ** |       holder:                                                 | **
  14. ** |                                                               | **
  15. ** |                          Eric Martz                           | **
  16. ** |                         POWER  TOOLS                          | **
  17. ** |                    48 Hunter's Hill Circle                    | **
  18. ** |                      Amherst MA 01002 USA                     | **
  19. ** |                                                               | **
  20. ** |                                                               | **
  21. ** `---------------------------------------------------------------' **
  22. \*********************************************************************/
  23.  
  24. #include "rap.h"
  25.  
  26. /*------------------------------------------------------------------------*/
  27. init_ss() {
  28.     FILE *fpss;
  29.     int c, junk;
  30.  
  31.     Ss_pair_cnt = 0;
  32.     Oldnewtot = 0;
  33.  
  34.     /* GET STRINGS FROM SSn */
  35.     fpss = fopen(Ss_name,"r-");
  36.     if (!fpss) {
  37.         fprintf(STDERR,"Warning: file \"%s\" does not exist.\n",Ss_name);
  38.         return(0);
  39.     }
  40.     fprintf(STDERR,"Reading file \"%s\":\n",Ss_name);
  41.  
  42.     /* PROCESS CONTENTS OF *.SS FILE */
  43.     /* ONLY BLANK LINES, COMMENTS ALLOWED BEFORE COMMANDS */
  44.  
  45.     FOREVER {
  46.         c = getc(fpss);
  47.         switch (c) {
  48.             case '\n':
  49.             case '\r':
  50.                 /* INGNORE BLANK LINES */
  51.                 break;
  52.             case '!':
  53.                 /* READ AND IGNORE REMAINDER OF LINE */
  54.                 rapgets(Inbuf,fpss);
  55.                 break;
  56.             case COMMAND:
  57.                 ungetc(c,fpss);
  58.                 rapgets(Inbuf,fpss);
  59.                 de_comment(Inbuf);
  60.                 command(Inbuf, Nxname, &junk);
  61.                 break;
  62.             default:
  63.                 goto pastcomms;
  64.         }
  65.     }
  66. pastcomms:
  67.     if (c EQ EOF) c = CPMEOF;
  68.     ungetc(c,fpss);
  69.  
  70.     /* GET SUBSTITUTION PAIRS AND HARDWARE INFO */
  71.     if (!getpairs(fpss, Ss_name)) {
  72.         fprintf(STDERR, "ERROR in SS file:\n");
  73.         fprintf(STDERR, "  }u+}, }u-}, }b+}, or }b-} is missing.\n");
  74.     }
  75.  
  76.     fpfree(fpss);
  77.  
  78.     /* put "}RF}" in Oldstring[0] for use in ss() */
  79.     Inbuf[0] = Post_ss_delim;
  80.     Inbuf[1] = NULL;
  81.     strcat(Inbuf,"RF");
  82.     Inbuf[3] = Post_ss_delim;
  83.     Inbuf[4] = NULL;
  84.     Oldstring[0] = pack(Oldnewbuf,&Oldnewtot,MAXPACK,Inbuf);
  85. }
  86. /*------------------------------------------------------------------------*/
  87. ss(bf,mark)
  88.     char *bf, *mark;
  89.     {
  90.     char double_delim[3], newrf[5], prompt[3], delim_str[2], wait[7];
  91.     char up[3], down[3], under[3], bold[3], arab[8];
  92.     int start, stop, i, pr;
  93.  
  94.     delim_str[0] = *mark;
  95.     delim_str[1] = NULL;
  96.     wait[0] = Post_ss_delim;
  97.     wait[1] = NULL;
  98.     strcpy(wait, "wait");
  99.     wait[5] = Post_ss_delim;
  100.     wait[6] = NULL;
  101.     up[0] = down[0] = under[0] = bold[0] = Pre_ss_delim;
  102.     up[1] = 'u';
  103.     down[1] = 'd';
  104.     under[1] = '_';
  105.     bold[1] = '!';
  106.     up[2] = down[2] = under[2] = bold[2] = NULL;
  107.  
  108.  
  109. #ifdef DEBUG
  110.     if (Debug) {
  111.         fprintf(STDERR,"Entering ss%s:",mark);
  112.         checkbig("",bf);
  113.     }
  114. #endif
  115.     stop = 0;
  116.  
  117.     /* CONVERT CONSOLE PROMPT DELIMITERS */
  118.     prompt[0] = Post_ss_delim;
  119.     prompt[1] = '"';
  120.     prompt[2] = NULL;
  121.     while (mark[0] EQ Post_ss_delim
  122.         AND (i = instr(0, bf, prompt)) NE ERROR) {
  123.  
  124.         bf[i] = c1(PROMPT);
  125.         pr = i;
  126.         do {    /* SHIFT DOWN TO DELETE " */
  127.             i++;
  128.             bf[i] = bf[i+1];
  129.         } while (bf[i]);
  130.  
  131.         i = instr(pr, bf, delim_str);
  132.         if (i EQ ERROR) {
  133.             i = strlen(bf);
  134.             bf[i+1] = NULL;
  135.         }
  136.         bf[i] = c1(PROMPT);
  137.     }
  138.  
  139.     /* SQUEEZE BLANKS OUT OF ORIGINAL STRINGS FOR SUBSTITUTION */
  140.     while ((start = instr(stop, bf, mark)) NE -1) {
  141.  
  142.         stop = instr(start+1, bf, mark);
  143.         if (stop EQ -1) {
  144.             fprintf(STDERR,
  145. "Incomplete \"%s\"-delimited substitution string at line %d:\n",
  146.                 mark,In_linecnt);
  147.             checkbig("",bf);
  148.             exit(0);
  149.         }
  150.         stop = stop +1;
  151.             /* -(delchars(bf+start, bf+stop, " ")); defeated 2.21 */
  152.     }
  153. #ifdef DEBUG
  154.     if (Debug AND stop NE 0) checkbig("",bf);
  155. #endif
  156.     /* NO SUBSTITUTIONS NECESSARY */
  157.     /* IF \\ THEN SS IS NOT CALLED UNLESS \\ OR Press_all */
  158.     if (stop EQ 0 AND mark[0] EQ Post_ss_delim) return(0);
  159.  
  160.     /* INSTALL AUTO-INCREMENTING ARABIC NUMBER IF NEEDED */
  161.     if (mark[0] EQ Pre_ss_delim) {
  162.         arab[0] = Pre_ss_delim;
  163.         arab[1] = '#';
  164.         arab[2] = NULL;
  165.         if ((i=instr(0, bf, arab)) NE ERROR) {
  166.             if (bf[i+2] EQ '+') Arabic++;
  167.             sprintf(newrf, "%d", Arabic);
  168.             arab[2] = '+';
  169.             arab[3] = Pre_ss_delim;
  170.             arab[4] = NULL;
  171.             if (bf[i+2] EQ '+') substitute(bf, arab, newrf);
  172.             else {
  173.                 arab[2] = Pre_ss_delim;
  174.                 arab[3] = NULL;
  175.                 substitute(bf, arab, newrf);
  176.             }
  177.         }
  178.     }
  179.     
  180.     /* EXPAND SUPERSCRIPT/SUBSCRIPT MACRO */
  181.     if (mark[0] EQ Pre_ss_delim) {
  182.         start = 0;
  183.         while ((start=instr(start,bf, up)) NE ERROR)
  184.             updown(bf,start,'u');
  185.         start = 0;
  186.         while ((start=instr(start,bf, down)) NE ERROR)
  187.             updown(bf,start,'d');
  188.         start = 0;
  189.         while ((start=instr(start,bf, under)) NE ERROR)
  190.             updown(bf,start,'_');
  191.         start = 0;
  192.         while ((start=instr(start,bf, bold)) NE ERROR)
  193.             updown(bf,start,'!');
  194.     }
  195.     
  196.     /* WAIT */
  197.     if (mark[0] EQ Post_ss_delim) substitute(bf, wait, WAIT);
  198.     
  199.     /* MAIN SUBSTITUTION LOOP */
  200.     for (i = 1; i <= Ss_pair_cnt; i++) {
  201.         if (*Oldstring[i] EQ mark[0]
  202.             OR
  203.  
  204.             /* FOR NON-DELIMITED ORIGINAL STRINGS (WHICH MUST HAVE BEEN
  205.                 SUFFIXED WITH '@' IN THE SSn FILE) */
  206.  
  207.             (mark[0] EQ Pre_ss_delim
  208.             AND
  209.             *Oldstring[i] NE Post_ss_delim))
  210.  
  211.             substitute (bf, Oldstring[i], Newstring[i]);
  212.     }
  213.  
  214.     /* RESTORE FONT IF REQUESTED */
  215.     if (mark[0] EQ Post_ss_delim AND Font_o) {
  216.         sprintf(newrf, "%s%d%s", Font_o, Font, Font_c);
  217.         substitute(bf, Oldstring[0], newrf);
  218.             /* Oldstring[0] was initialized to "}RF}" in init_ss */
  219.     }
  220.     
  221.     /* CONVERT }} TO } AND {{ TO { */
  222.     if (bf[0] NE COMMAND) {
  223.         double_delim[0] = double_delim[1] = mark[0];
  224.         double_delim[2] = NULL;
  225.         substitute (bf, double_delim, mark);
  226.     }
  227.  
  228. #ifdef DEBUG
  229.     if (Debug) checkbig("ss done:",bf);
  230. #endif
  231. }
  232.  
  233. /*------------------------------------------------------------------------*/
  234. expantab(bf)
  235.     char *bf;
  236.     {
  237.     int b, t, inss, pos;
  238.     char tmp[BIGBUF];
  239.     
  240.     /* IF THERE'S NO TAB, RETURN */
  241.     if ((b=instr(0,bf,"\t")) EQ -1) return(0);
  242.     
  243.     /* IF THERE'S A TAB, TRANSFER PRE-TAB STRING TO TMP */
  244.     for (t=0, pos=0; t<b; t++, pos++) tmp[t] = bf[t];
  245.     t = b;
  246.     
  247.     /* EXPAND TABS, TAKING POST-SUBSTITUTIONS INTO ACCOUNT */
  248.     inss = 0;
  249.     while (bf[b]) {
  250.         if (bf[b] NE TAB) {
  251.             if (bf[b] EQ Post_ss_delim) {
  252.                 if (!inss) inss = YES; /* OPENING DELIM */
  253.                 if (!inss) pos++;
  254.                 if (inss) inss = NO; /* CLOSING DELIM */
  255.             }
  256.             else if (!inss) pos++;
  257.             tmp[t++] = bf[b++];
  258.         }
  259.         else {
  260.             do {
  261.                 tmp[t++] = SPACE;
  262.                 pos++;
  263.             } while (pos%Tabval);
  264.             b++;
  265.         }
  266.     }
  267.     tmp[t] = NULL;
  268.     strcpy(bf, tmp);
  269. }
  270. /*------------------------------------------------------------------------*/
  271. /* FNNLGETS = File-No-NewLine-GET-String
  272. SAME AS FGETS BUT STRING LEFT IN S IS STRIPPED OF THE TERMINAL NEWLINE.
  273. RAPGETS: SETS Press_flg +  FOR EVEN NUMBER OF Pre_ss_delim, - FOR ODD,
  274. 0 FOR NONE. */
  275.  
  276. rapgets(s,fp)
  277.     char *s;
  278.     FILE *fp;
  279.     {
  280.     char *p;
  281.     int length;
  282.     Press_flg = 0;
  283.     if ((length = biggets(s,fp)) EQ 0) return(0);
  284.     In_linecnt++;
  285.     if (Pre_ss) for(p = s; *p; p++) {
  286.         if (*p EQ Pre_ss_delim) {
  287.             if (Press_flg >= 0) Press_flg = -1;
  288.             else Press_flg = 1;
  289.         }
  290.     }
  291.     /* TRIMS OFF TERMINAL NEWLINE */
  292.     if (s[length-1] EQ '\n') s[length-1] = NULL;
  293.     return(1);
  294. }
  295. /*--------------------------------------------------------------------